package custom.base.http;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import custom.base.http.data.BaseNetCode;
import custom.base.http.data.IPCache;
import custom.base.utils.TxtUtil;

/**
 * Created by luozi on 2016/7/13.
 */
public class HttpProxy implements IPCache, BaseNetCode {

    public Context context;

    private SharedPreferences mPrefer;
    private SharedPreferences.Editor editor;

    public HttpProxy(Context context) {
        this.context = context;
        mPrefer = context.getSharedPreferences(PREFER_NAME,
                Context.MODE_PRIVATE);
        editor = mPrefer.edit();
    }

    /**
     * 缓存地址，避免每次都从文件当中去取
     */
    public static String cacheUrl = "";

    public static void clearCacheUrl() {
        HttpProxy.cacheUrl = "";
    }

    @Override
    public String getBaseUrl() {
        String ip;
        if (TxtUtil.isEmpty(cacheUrl)) {
            String tmp = getString(PRE_BASE_URL, "");
            if (!TextUtils.isEmpty(tmp)) {
                ip = tmp;
            } else {
                ip = BASE_URL;
            }
        } else {
            ip = cacheUrl;
        }
        return ip;
    }

    /**
     * 得到app请求地址
     */
    public String getAPPUrl() {
        return getBaseUrl();
//        return URL_PREFIX + getBaseUrl() + ":" + getBaseModleName() + "/";
    }


    @Override
    public void setBaseUrl(String url) {
        if (!TextUtils.isEmpty(url))
            putString(PRE_BASE_URL, url);
    }

    /**
     * 存放String 值
     */
    public void putString(String name, String values) {
        editor.putString(name, values);
        editor.commit();
    }


    /**
     * 得到xml文件中的键值对名 如果没有值则默认为“”
     */
    public String getString(String name, String defValue) {
        return mPrefer.getString(name, defValue);
    }
}
